package com.lzb.play.dialog;

import android.content.Context;
import android.media.AudioManager;
import android.view.Gravity;
import android.view.View;
import android.widget.SeekBar;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.VolumeUtils;
import com.lzb.play.R;
import com.lzb.play.basic.dialog.BaseSmartDialog;
import com.lzb.play.databinding.DialogVolumeSetBinding;

public class VolumeSetDialog extends BaseSmartDialog<DialogVolumeSetBinding> implements View.OnClickListener {

    private int max, min, now, progress;

    public VolumeSetDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_volume_set;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.ivMin.setOnClickListener(this);
        mDataBinding.ivAdd.setOnClickListener(this);

        max = VolumeUtils.getMaxVolume(AudioManager.STREAM_MUSIC);
        min = VolumeUtils.getMinVolume(AudioManager.STREAM_MUSIC);
        now = VolumeUtils.getVolume(AudioManager.STREAM_MUSIC);
        progress = now * 100 / (max - min);
        mDataBinding.tvProgress.setText(progress + getContext().getString(R.string.unit_percent));
        mDataBinding.sbProgress.setProgress(progress);
        mDataBinding.sbProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                progress = i;
                setVolume();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        } else if (id == R.id.ivMin) {
            if (progress == 0) return;
            progress--;
            setVolume();
        } else if (id == R.id.ivAdd) {
            if (progress == 100) return;
            progress++;
            setVolume();
        }
    }

    private void setVolume() {
        mDataBinding.tvProgress.setText(progress + getContext().getString(R.string.unit_percent));
        float value = calSeekBarRealValue(mDataBinding.sbProgress, progress);
        VolumeUtils.setVolume(AudioManager.STREAM_MUSIC, (int) value, 0);
    }

    private float calSeekBarRealValue(SeekBar seekBar, int progress) {
        int progressRange = seekBar.getMax();
        float value = progress * 1.0f / progressRange * (max - min) + min;
        return value;
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void click(String name);
    }
}
